package YAPclient;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

import YAPenvironment.YAPconstant;
import YAPenvironment.YAPconstant.Command;


/**
 * This class provides a panel with several sliders, allowing the user to create a new playground by
 * specifying its number of walls and grade of irregularity, as well as
 * setting the number of lives a player should have.
 */

public class CreatePanel extends JPanel implements ActionListener{
	
	private static final long serialVersionUID = -4363317061322347681L;

	String sep = YAPconstant.C_SEPARATOR; 
	
	private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
	private javax.swing.JSlider jsLives;
    private javax.swing.JSlider jsNumberOfWalls;
    private javax.swing.JSlider jsRadSynch;
    private javax.swing.JButton jbCreate;

	
    /**
     * Creates a new instance of the class CreatePanel
     */
	public CreatePanel(){

		UIManager.getLookAndFeelDefaults().put("Slider.paintValue", false);
		jsRadSynch = new javax.swing.JSlider();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jsLives = new javax.swing.JSlider();
        jbCreate = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jsNumberOfWalls = new javax.swing.JSlider();
        
        jLabel1.setText("radial irregularity:");
        
        jsRadSynch.setMaximum(10);
        jsRadSynch.setMajorTickSpacing(5);
        jsRadSynch.setMinorTickSpacing(1);
        jsRadSynch.setPaintTicks(true);
        jsRadSynch.setSnapToTicks(true);
        jsRadSynch.setValue(0);
        Hashtable<Integer,JLabel> labelTable = new Hashtable<Integer,JLabel>();
        labelTable.put(0,new JLabel("0"));
        labelTable.put(5, new JLabel("0.5"));
        labelTable.put(10,new JLabel("1.0"));
        jsRadSynch.setLabelTable( labelTable );
        jsRadSynch.setPaintLabels(true);

        jLabel2.setText("number of lives:");
        
        jsLives.setMinimum(1);
        jsLives.setMaximum(10);
        jsLives.setMajorTickSpacing(3);
        jsLives.setMinorTickSpacing(1);
        jsLives.setPaintTicks(true);
        jsLives.setSnapToTicks(true);
        jsLives.setValue(YAPconstant.C_LIVES); //the default numOfLives
        jsLives.setPaintLabels(true);

        jbCreate.setText("create");
        jbCreate.addActionListener(this);

        jLabel3.setText("number of walls:");

        jsNumberOfWalls.setMaximum(YAPconstant.C_MAXWALLS);
        jsNumberOfWalls.setMinimum(YAPconstant.C_MINWALLS);
        jsNumberOfWalls.setValue(DataModul.gInfo.getMaxPlayerCount());
        jsNumberOfWalls.setMajorTickSpacing(3);
        jsNumberOfWalls.setMinorTickSpacing(1);
        jsNumberOfWalls.setPaintTicks(true);
        jsNumberOfWalls.setSnapToTicks(true);
        jsNumberOfWalls.setPaintLabels(true);
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createSequentialGroup()
            	.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jsRadSynch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jsLives, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jsNumberOfWalls, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                 .addGap(25,25,25)
                 .addComponent(jbCreate, javax.swing.GroupLayout.PREFERRED_SIZE,  javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
        layout.setVerticalGroup(
        	layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jsRadSynch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jsLives, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jsNumberOfWalls, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(jbCreate));

	}
	

	/**
	 * Creates a new playground by passing the values specified by the user to the server (using the {@link ParseThread}'s writeMessage method)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==jbCreate){
			DataModul.parseThread.writeMessage(Command.createPlayField+sep+DataModul.gInfo.getName()+sep+jsNumberOfWalls.getValue()
					+sep+(double)jsRadSynch.getValue()/10+sep+jsLives.getValue());
			
		}
		
	}
	
	
	/**
	 * Disables the CreatePanel, i.e. disables all its subcomponents (used for non-host players)
	 */
	public void disable(){
		jsRadSynch.setEnabled(false);
		jsLives.setEnabled(false);
		jsNumberOfWalls.setEnabled(false);
		jbCreate.setEnabled(false);
		jLabel1.setEnabled(false);
		jLabel2.setEnabled(false);
		jLabel3.setEnabled(false);
	}
	
	
	/**
	 * @param i	Position where to set the knob of the slider jsLives
	 */
	public void setLives(int i){
		jsLives.setValue(i);
	}
	
	
	/**
	 * @param i Position where to set the knob of the slider jsRadSynch
	 */
	public void setRadSync(int i){
		jsRadSynch.setValue(i);
	}
	
	
	/**
	 * @param i Position where to set the knob of the slider jsNumberOfwalls
	 */
	public void setNumberOfWalls(int i){
	    jsNumberOfWalls.setValue(i);
	}
}
